5.10. Recall that all structured blocks modified by an unnamed critical directive form a single critical section. What happens if we have a number of atomic directives in which different variables are being modified? Are they all treated as a single critical section? We can write a small program that tries to determine this. The idea is to have all the threads simultaneously execute something like the following code int i; double my sum = 0.0; for (i = 0; i < n; i++) # pragma omp atomic my sum += sin(i); We can do this by modifying the code by a parallel directive: # pragma omp parallel num threads(thread count) { int i; double my sum = 0.0; for (i = 0; i < n; i++) # pragma omp atomic my sum += sin(i); } Note that since my sum and i are declared in the parallel block, each thread has its own private copy. Now if we time this code for large n when thread count D 1 and we also time it when thread count > 1, then as long as thread count is less than the number of available cores, the run-time for the single-threaded run should be roughly the same as the time for the multithreaded run if the different threads' executions of my sum += sin(i) are treated as different critical sections. On the other hand, if the different executions of my sum += sin(i) are all treated as a single critical section, the multithreaded run should be much slower than the single-threaded run. Write an OpenMP program that implements this test. Does your implementation of OpenMP allow simultaneous execution of updates to different variables when the updates are protected by atomic directives? | |
| View Solution | |
| << Back | Next >> |